Selenium 4 New Features
Why Selenium 4 Matters (Real Project Context)β
Selenium 4 is not just a version bumpβit modernizes Selenium to align with W3C WebDriver standard, improves stability, and adds developer-focused features that reduce flaky tests.
If youβre building new frameworks today, Selenium 4 is mandatory.
1. W3C WebDriver Compliance (Most Important)β
Selenium 4 is fully compliant with the W3C WebDriver standard.
Why This Mattersβ
- Consistent behavior across browsers
- Fewer browser-specific hacks
- Better Grid and cloud compatibility
Real Impactβ
- Fewer random failures
- More predictable interactions
- Cleaner browser logs
2. Relative Locators (UI-Focused Feature)β
Locate elements based on their position relative to other elements.
WebElement password = driver.findElement(By.id("password"));
WebElement username = driver.findElement(
RelativeLocator.with(By.tagName("input")).above(password)
);
When Usefulβ
- Forms without unique IDs
- Dynamic layouts
- Reducing complex XPath
β οΈ Use sparinglyβIDs and stable locators are still preferred.
3. New Window & Tab Handling APIβ
Clear and explicit control for tabs/windows.
driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://example.com");
Benefitsβ
- No JavaScript hacks
- Cleaner multi-window tests
4. Native Shadow DOM Supportβ
Selenium 4 provides native access to open Shadow DOM.
WebElement host = driver.findElement(By.cssSelector("custom-element"));
SearchContext shadowRoot = host.getShadowRoot();
Removes dependency on JavaScript workarounds.
5. Improved Actions APIβ
Actions are now more reliable and aligned with W3C input actions.
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();
Especially improves:
- Hover
- Drag-and-drop
- Slider interactions
6. Browser Options Unificationβ
Options classes now replace DesiredCapabilities.
ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);
Cleaner, safer, Selenium 4βonly approach.
7. Better Error Messages & Loggingβ
Selenium 4 provides:
- Clearer stack traces
- Better timeout messages
- Improved debugging experience
This directly reduces debugging time in CI.
8. DevTools Integration (Awareness)β
Limited Chrome DevTools Protocol (CDP) integration.
Use cases:
- Network interception (basic)
- Performance metrics
β οΈ Advanced CDP usage is out of scope for most UI automation.
What Selenium 4 Does NOT Changeβ
β Selenium is still not a visual testing tool
β Selenium does not test APIs
β Selenium does not replace frameworks
Common Migration Mistakes ββ
- Continuing to use
DesiredCapabilities - Assuming Selenium 4 fixes bad waits
- Overusing relative locators
- Ignoring Shadow DOM changes
- Not upgrading browser drivers
Best Practices β β
- Use Selenium 4 APIs only
- Remove deprecated patterns
- Update Grid configuration
- Validate framework compatibility
- Train team on new features
Interview Notes π―β
Q: Biggest improvement in Selenium 4?
A: Full W3C WebDriver compliance.
Q: Are DesiredCapabilities used in Selenium 4?
A: No, they are deprecated.
Q: Does Selenium 4 support Shadow DOM?
A: Yes, open Shadow DOM natively.
Real-Project Tip π‘β
Selenium 4 improves stabilityβbut only if your framework design and waits are correct.
Summaryβ
- Selenium 4 aligns with modern browsers
- Adds powerful UI-focused features
- Removes legacy APIs
- Essential for enterprise automation